package net.xyt.report.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.xyt.report.util.ReportDataSource;
import net.xyt.report.util.ReportUtils;

@Controller
public class TestController {
  
	@GetMapping("/test/export")    
	public void getReportByParam(@RequestParam("type") String type,
			                     HttpServletResponse response) {
	try {
		//1.加载模板文件
        ClassPathResource resource = new ClassPathResource("jasper" + File.separator + "param.jasper");
        InputStream jasperStream = resource.getInputStream();
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
        //2.获取数据源
        List<Map<String,Object>> list = new ArrayList<>();
		Map<String,Object> parameters1 = new HashMap<>();        //获取文件流
        parameters1.put("user_id", 1);
        parameters1.put("user_nm", "张三");
        parameters1.put("password", "123456");
        parameters1.put("cardno", "451551198905025615");
        
        Map<String,Object> parameters2 = new HashMap<>();        //获取文件流
        parameters2.put("user_id", 2);
        parameters2.put("user_nm", "李四");
        parameters2.put("password", "123456");
        parameters2.put("cardno", "451551198905025615");
        list.add(parameters1);
        list.add(parameters2);
        //3.根据数据类型填充模板文件jasper
        JasperPrint jasperPrint = ReportDataSource.dealDataParam(jasperReport, parameters2);
        //JasperPrint jasperPrint = ReportDataSource.dealDataList(jasperReport, list);
        //4.导出文件
        String filename="测试";	
		ReportUtils.exportFileByType(type, filename, jasperPrint, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
    }

	@GetMapping("/test/chart")
    public void getReportChart(@RequestParam("type") String type, HttpServletResponse response) {
		
		try {
			//1.加载模板文件
	        ClassPathResource resource = new ClassPathResource("jasper" + File.separator + "citynum.jasper");
	        InputStream jasperStream = resource.getInputStream();
	        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperStream);
	        //2.获取数据源
	        List<Map<String,Object>> list = new ArrayList<>();
	        Map<String,Object> param1 = new HashMap<>();
	        param1.put("province", "湖北省");
	        param1.put("category", "IT行业");
	        param1.put("amount", 8.75);
	        Map<String,Object> param2 = new HashMap<>();
	        param2.put("province", "湖南省");
	        param2.put("category", "IT行业");
	        param2.put("amount", 7.75);
	        Map<String,Object> param3 = new HashMap<>();
	        param3.put("province", "河南省");
	        param3.put("category", "IT行业");
	        param3.put("amount", 8.15);
	        Map<String,Object> param4 = new HashMap<>();
	        param4.put("province", "湖北省");
	        param4.put("category", "制造业");
	        param4.put("amount", 5.75);
	        Map<String,Object> param5 = new HashMap<>();
	        param5.put("province", "湖南省");
	        param5.put("category", "制造业");
	        param5.put("amount", 6.15);
	        Map<String,Object> param6 = new HashMap<>();
	        param6.put("province", "河南省");
	        param6.put("category", "制造业");
	        param6.put("amount", 5.95);
	        Map<String,Object> param7 = new HashMap<>();
	        param7.put("province", "湖北省");
	        param7.put("category", "教育业");
	        param7.put("amount", 9.75);
	        Map<String,Object> param8 = new HashMap<>();
	        param8.put("province", "湖南省");
	        param8.put("category", "教育业");
	        param8.put("amount", 9.15);
	        Map<String,Object> param9 = new HashMap<>();
	        param9.put("province", "河南省");
	        param9.put("category", "教育业");
	        param9.put("amount", 8.95);
	        list.add(param1);
	        list.add(param2);
	        list.add(param3);
	        list.add(param4);
	        list.add(param5);
	        list.add(param6);
	        list.add(param7);
	        list.add(param8);
	        list.add(param9);
	        //3.根据数据类型填充模板文件jasper
	        JasperPrint jasperPrint = ReportDataSource.dealDataList(jasperReport, list);
	       //4.导出文件
	        String filename="各省指数";	
			ReportUtils.exportFileByType(type, filename, jasperPrint, response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JRException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
    }
}
